﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using SilverlightEventing.IdentityMine.Windows.Eventing;
using System.Diagnostics;

namespace IdentityMine.Windows.Eventing
{
    public class EventManagerEx
    {
        static List<RoutedEventEx> _globalList = new List<RoutedEventEx>();
        static Dictionary<Type, RoutedEventHandlerDataCollection> _globalTypeList = new Dictionary<Type, RoutedEventHandlerDataCollection>();

        public static RoutedEventEx[] GetRoutedEvents()
        {
            return _globalList.ToArray();
        }

        public static RoutedEventEx[] GetRoutedEventsForOwner(Type ownerType)
        {
            List<RoutedEventEx> lst = new List<RoutedEventEx>();

            foreach (RoutedEventEx evnt in _globalList)
            {
                if (CheckIsOfOwnerType(evnt, ownerType))
                {
                    lst.Add(evnt);
                }
            }

            return lst.ToArray();
        }

        public static RoutedEventHandlerDataCollection GetForType(Type ownerType)
        {
            RoutedEventHandlerDataCollection retCol = new RoutedEventHandlerDataCollection();

            Type itType = ownerType;

            while (itType != null)
            {
                if (_globalTypeList.ContainsKey(itType))
                {
                    retCol.AddRange(_globalTypeList[itType]);
                }

                Type savedType = itType;
                itType = itType.BaseType;
                if (itType == savedType) itType = null;
            }

            return retCol;
        }

        protected static bool CheckIsOfOwnerType(RoutedEventEx evnt, Type ownerType)
        {
            bool bIsOfType = false;

            if (evnt.OwnerType == ownerType || evnt.OwnerType.IsSubclassOf(ownerType))
            {
                bIsOfType = true;
            }

            return bIsOfType;
        }

        public static void RegisterClassHandler(Type classType, RoutedEventEx routedEvent, Delegate handler)
        {
            RegisterClassHandler(classType, routedEvent, handler, false);
        }

        public static void RegisterClassHandler(Type classType, RoutedEventEx routedEvent, Delegate handler, bool handlesEventsToo)
        {
            RoutedEventHandlerDataCollection coll = null;

            if (!_globalTypeList.ContainsKey(classType))
            {
                coll = new RoutedEventHandlerDataCollection();
                _globalTypeList.Add(classType, coll);
            }
            else
            {
                coll = _globalTypeList[classType];
            }

            coll.AddEntry(new RoutedEventHandlerData(routedEvent, null, handler, handlesEventsToo));
        }

        public static RoutedEventEx RegisterRoutedEvent(string name, RoutingStrategy routingStrategy, Type handlerType, Type ownerType)
        {
            RoutedEventEx evnt = new RoutedEventEx(name, routingStrategy, handlerType, ownerType);
            _globalList.Add(evnt);

            return evnt;
        }

    }
}
